﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using AlarmManagePlatform.Model.DB;

namespace AlarmManagePlatform.DataAccess
{
    internal class tb_AlarminstrumentInfo
    {
        public bool CheckInstrumentIsExist(string factoryNumber)
        {
            Relations.DBUtility.SqlServer.SqlDb sqlDb = null;
            try
            {
                string sqlScript;
                sqlScript = string.Format("select InstrumentId from tb_AlarminstrumentInfo where FactoryNumber = '{0}';", factoryNumber);
                sqlDb = Static.Methods.OpenDataBase();
                if (sqlDb.RunSqlScript(sqlScript).Rows.Count > 0)
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {
                throw new Exception("检查报警器是否存在失败，错误信息：" + ex.Message);
            }
            finally
            {
                if (sqlDb != null)
                    Static.Methods.CloseDataBase(sqlDb);
            }
        }

        //_Result.Add("BeginTime", checkDateTime.Checked? dtBegin.Value.ToString(Static.ParaInfo.DateFormatShort + " 00:00:00") : string.Empty);
        //    _Result.Add("FinishTime", checkDateTime.Checked? dtFinish.Value.ToString(Static.ParaInfo.DateFormatShort + " 23:59:59") : string.Empty);
        //    _Result.Add("FactoryNumber", checkFactoryNumber.Checked? txtFactoryNumber.Text.ToString() : string.Empty);
        //    _Result.Add("Manufacturer", checkManufacturer.Checked? txtManufacturer.Text.ToString() : string.Empty);
        //        _Result.Add("GasTypeList", "%" + string.Join("%", _GasType.ToArray()) + "%");
        //        _Result.Add("SampingMode", string.Empty);
        //    _Result.Add("UserId", checkUser.Checked? cmbUser.SelectedValue.ToString() : string.Empty);
        //    _Result.Add("SubmitUnit", checkSubmitUnit.Checked? cmbSubmitUnit.Text.ToString() : string.Empty);

        internal int GetInstrumentCount(Dictionary<string, string> logisticList)
        {
            Relations.DBUtility.SqlServer.SqlDb _SqlDb = null;
            int _Result;
            string _Scalar = string.Empty;
            string _SqlScript;
            try
            {
                _Result = -1;

                _SqlScript = "Select Count(*) From tb_AlarminstrumentInfo";
                _SqlScript += $"    Where [InstrumentId] > '0'";

                //出厂编号
                if (logisticList.ContainsKey("FactoryNumber") && !string.IsNullOrEmpty(logisticList["FactoryNumber"]))
                {
                    _SqlScript += $"   And [FactoryNumber] like '%{logisticList["FactoryNumber"]}%'";
                }

                //生产厂家
                if (logisticList.ContainsKey("Manufacturer") && !string.IsNullOrEmpty(logisticList["Manufacturer"]))
                {
                    _SqlScript += $"   And [Manufacturer] like '%{logisticList["Manufacturer"]}%'";
                }

                //气体列表
                if (logisticList.ContainsKey("GasTypeList") && !string.IsNullOrEmpty(logisticList["GasTypeList"]))
                {
                    _SqlScript += $"   And [GasTypeList] like '%{logisticList["GasTypeList"]}%'";
                }

                //采样方式
                if (logisticList.ContainsKey("SampingMode") && !string.IsNullOrEmpty(logisticList["SampingMode"]))
                {
                    _SqlScript += $"   And [SampingMode] = '{logisticList["SampingMode"]}'";
                }

                //送检单位
                if (logisticList.ContainsKey("SubmitUnit") && !string.IsNullOrEmpty(logisticList["SubmitUnit"]))
                {
                    _SqlScript += $"   And [SubmitUnit] = '{logisticList["SubmitUnit"]}'";
                }

                //InstrumentId
                _SqlScript += $"    And [InstrumentId] In ( ";

                _SqlScript += $"    Select Distinct [InstrumentId] From  [tb_AlarminstrumentHisData]  Where [Id] > 0 ";

                if (logisticList.ContainsKey("BeginTime") && !string.IsNullOrEmpty(logisticList["BeginTime"]))
                {
                    _SqlScript += $"   And [CreateDate] >= '{logisticList["BeginTime"]}'";
                }

                if (logisticList.ContainsKey("FinishTime") && !string.IsNullOrEmpty(logisticList["FinishTime"]))
                {
                    _SqlScript += $"   And [CreateDate] <= '{logisticList["FinishTime"]}'";
                }

                if (logisticList.ContainsKey("UserId") && !string.IsNullOrEmpty(logisticList["UserId"]))
                {
                    _SqlScript += $"   And [UserId] = {logisticList["UserId"]}";
                }

                _SqlScript += $"    )  ";


                _SqlDb = Static.Methods.OpenDataBase();

                _Scalar = _SqlDb.RunSqlScriptReturnScalar(_SqlScript);

                if (string.IsNullOrEmpty(_Scalar))
                {
                    _Result = 0;
                }
                else
                {
                    _Result = Convert.ToInt32(_Scalar);
                }

                return _Result;
            }
            catch (Exception ex)
            {
                throw new Exception("获取报警器数量失败，错误信息：" + ex.Message);
            }
            finally
            {
                if (_SqlDb != null)
                    Static.Methods.CloseDataBase(_SqlDb);
            }
        }

        internal List<Model.DB.tb_AlarminstrumentInfo> GetInstrumentList()
        {
            Relations.DBUtility.SqlServer.SqlDb _SqlDb = null;
            List<Model.DB.tb_AlarminstrumentInfo> _Result;
            DataTable _DataTable;
            string _SqlScript;
            try
            {
                _Result = new List<Model.DB.tb_AlarminstrumentInfo>();

                _SqlScript = "Select * From tb_AlarminstrumentInfo Order By FactoryNumber";
                _SqlDb = Static.Methods.OpenDataBase();
                _DataTable = _SqlDb.RunSqlScript(_SqlScript);

                //for (int i = 0; i < _DataTable.Rows.Count; i++)
                //{
                //    _tb_AlarminstrumentInfo = null;
                //    _tb_AlarminstrumentInfo = new Model.DB.tb_AlarminstrumentInfo();
                //    _tb_AlarminstrumentInfo.InstrumentId = _DataTable.Rows[i]["InstrumentId"].ToString();
                //    _tb_AlarminstrumentInfo.InstrumentName = _DataTable.Rows[i]["InstrumentName"].ToString();
                //    _tb_AlarminstrumentInfo.FactoryNumber = _DataTable.Rows[i]["FactoryNumber"].ToString();
                //    _tb_AlarminstrumentInfo.Model = _DataTable.Rows[i]["Model"].ToString();
                //    _tb_AlarminstrumentInfo.ContractNo = _DataTable.Rows[i]["ContractNo"].ToString();
                //    _tb_AlarminstrumentInfo.SubmitUnit = _DataTable.Rows[i]["SubmitUnit"].ToString();
                //    _tb_AlarminstrumentInfo.Manufacturer = _DataTable.Rows[i]["Manufacturer"].ToString();

                //    _tb_AlarminstrumentInfo.GasTypeList = _DataTable.Rows[i]["GasTypeList"].ToString();

                //    _GasType = _DataTable.Rows[i]["GasTypeList"].ToString();
                //    if (!string.IsNullOrWhiteSpace(_GasType))
                //    {
                //        _GasTypeList = _GasType.Split('|');
                //        foreach (string _Gas in _GasTypeList)
                //        {
                //            _tb_ParaInfo = new DataAccess.tb_ParaInfo().GetParaInfo(_Gas);
                //            _tb_AlarminstrumentInfo.GasInfoList.Add(_tb_ParaInfo);
                //            _tb_AlarminstrumentInfo.sGasTypeList += _tb_ParaInfo.Desc + "|";
                //        }
                //        if (_tb_AlarminstrumentInfo.sGasTypeList.EndsWith("|"))
                //            _tb_AlarminstrumentInfo.sGasTypeList = _tb_AlarminstrumentInfo.sGasTypeList.Substring(0, _tb_AlarminstrumentInfo.sGasTypeList.Length - 1);
                //    }

                //    _tb_AlarminstrumentInfo.SampingMode = (RelationsLMP.Model.SystemEnum.AlertorSampingMode)int.Parse(_DataTable.Rows[i]["SampingMode"].ToString());
                //    _tb_AlarminstrumentInfo.sSampingMode = Relations.Common.Methods.GetEnumDescription(_tb_AlarminstrumentInfo.SampingMode);

                //    _tb_AlarminstrumentInfo.ImageInfo = _DataTable.Rows[i]["ImageInfo"].ToString();
                //    _tb_AlarminstrumentInfo.InputUserId = int.Parse(_DataTable.Rows[i]["InputUserId"].ToString());
                //    _tb_AlarminstrumentInfo.GPSInfo = _DataTable.Rows[i]["GPSInfo"].ToString();

                //    if (!string.IsNullOrEmpty(_DataTable.Rows[i]["ModifyDate"].ToString()))
                //        _tb_AlarminstrumentInfo.ModifyDate = DateTime.Parse(_DataTable.Rows[i]["ModifyDate"].ToString());
                //    else
                //        _tb_AlarminstrumentInfo.ModifyDate = DateTime.Now;

                //    if (!string.IsNullOrEmpty(_DataTable.Rows[i]["CreateDate"].ToString()))
                //        _tb_AlarminstrumentInfo.CreateDate = DateTime.Parse(_DataTable.Rows[i]["CreateDate"].ToString());
                //    else
                //        _tb_AlarminstrumentInfo.CreateDate = DateTime.Now;

                //    _tb_AlarminstrumentInfo.sModifyDate = _tb_AlarminstrumentInfo.ModifyDate.ToString("yyyy/MM/dd HH:mm:ss");
                //    _tb_AlarminstrumentInfo.sCreateDate = _tb_AlarminstrumentInfo.CreateDate.ToString("yyyy/MM/dd HH:mm:ss");

                //    _Result.Add(_tb_AlarminstrumentInfo);
                //}

                _Result = DataTableToList(_DataTable);
                return _Result;
            }
            catch (Exception ex)
            {
                throw new Exception("获取报警器详细信息失败，错误信息：" + ex.Message);
            }
            finally
            {
                if (_SqlDb != null)
                    Static.Methods.CloseDataBase(_SqlDb);
            }
        }

        internal int GetAlarmInstrumentCount(Dictionary<string, string> logicList)
        {
            Relations.DBUtility.SqlServer.SqlDb _SqlDb = null;
            int _Result;
            string _Saclar = string.Empty;
            string _SqlScript;
            try
            {
                _Result = -1;

                _SqlScript = $"Select Count([InstrumentId]) From tb_AlarminstrumentInfo ";

                _SqlScript += $"    Where  [InstrumentId] > '0' ";

                if (logicList.ContainsKey("FactoryName") && !string.IsNullOrEmpty(logicList["FactoryName"]))
                {
                    _SqlScript += $"   And [FactoryNumber] Like '%{logicList["FactoryName"]}%'";
                }

                if (logicList.ContainsKey("Model") && !string.IsNullOrEmpty(logicList["Model"]))
                {
                    _SqlScript += $"   And [Model] Like '%{logicList["Model"]}%'";
                }

                if (logicList.ContainsKey("SubmitUnit") && !string.IsNullOrEmpty(logicList["SubmitUnit"]))
                {
                    _SqlScript += $"   And [SubmitUnit] = '{logicList["SubmitUnit"]}'";
                }

                if (logicList.ContainsKey("GasType") && !string.IsNullOrEmpty(logicList["GasType"]))
                {
                    _SqlScript += $"   And [GasTypeList] Like '%{logicList["GasType"]}%'";
                }

                _SqlDb = Static.Methods.OpenDataBase();

                _Saclar = _SqlDb.RunSqlScriptReturnScalar(_SqlScript);
                if (string.IsNullOrEmpty(_Saclar))
                {
                    _Result = 0;
                }
                else
                {
                    _Result = Convert.ToInt32(_Saclar);
                }

                return _Result;
            }
            catch (Exception ex)
            {
                throw new Exception("获取报警器数量失败，错误信息：" + ex.Message);
            }
            finally
            {
                if (_SqlDb != null)
                    Static.Methods.CloseDataBase(_SqlDb);
            }
        }

        internal List<Model.DB.tb_AlarminstrumentInfo> GetInstrumentList(int startIndex, int searchCount, Dictionary<string, string> logicList)
        {
            Relations.DBUtility.SqlServer.SqlDb _SqlDb = null;
            List<Model.DB.tb_AlarminstrumentInfo> _Result;
            DataTable _DataTable;
            string _SqlScript;
            try
            {
                _Result = new List<Model.DB.tb_AlarminstrumentInfo>();

                //_SqlScript = $"Select Top {searchCount} * From tb_AlarminstrumentInfo ";
                //_SqlScript += $"    Where FactoryNumber Not In (  Select Top {startIndex} FactoryNumber From  tb_AlarminstrumentInfo Order By FactoryNumber )";
                //_SqlScript += "    Order By FactoryNumber";

                _SqlScript = "      SELECT * FROM (SELECT *, ROW_NUMBER() OVER(ORDER BY [FactoryNumber] ASC) AS Num FROM tb_AlarminstrumentInfo ";

                _SqlScript += $"    Where  [InstrumentId] > '0' ";

                if (logicList.ContainsKey("FactoryName") && !string.IsNullOrEmpty(logicList["FactoryName"]))
                {
                    _SqlScript += $"   And [FactoryNumber] Like '%{logicList["FactoryName"]}%'";
                }

                if (logicList.ContainsKey("Model") && !string.IsNullOrEmpty(logicList["Model"]))
                {
                    _SqlScript += $"   And [Model] Like '%{logicList["Model"]}%'";
                }

                if (logicList.ContainsKey("SubmitUnit") && !string.IsNullOrEmpty(logicList["SubmitUnit"]))
                {
                    _SqlScript += $"   And [SubmitUnit] = '{logicList["SubmitUnit"]}'";
                }

                if (logicList.ContainsKey("GasType") && !string.IsNullOrEmpty(logicList["GasType"]))
                {
                    _SqlScript += $"   And [GasTypeList] Like '%{logicList["GasType"]}%'";
                }

                _SqlScript += $"    )AS TempTable";
                _SqlScript += $"    WHERE Num BETWEEN {startIndex} AND {startIndex + searchCount - 1}";



                _SqlDb = Static.Methods.OpenDataBase();
                _DataTable = _SqlDb.RunSqlScript(_SqlScript);

                _Result = DataTableToList(_DataTable);

                return _Result;
            }
            catch (Exception ex)
            {
                throw new Exception("获取报警器详细信息失败，错误信息：" + ex.Message);
            }
            finally
            {
                if (_SqlDb != null)
                    Static.Methods.CloseDataBase(_SqlDb);
            }
        }

        internal List<Model.DB.tb_AlarminstrumentInfo> GetInstrumentList_History(int startIndex, int searchCount, Dictionary<string, string> logisticList)
        {
            Relations.DBUtility.SqlServer.SqlDb _SqlDb = null;
            List<Model.DB.tb_AlarminstrumentInfo> _Result;
            DataTable _DataTable;
            string _SqlScript;
            try
            {
                _Result = new List<Model.DB.tb_AlarminstrumentInfo>();

                _SqlScript = "      SELECT * FROM (SELECT *, ROW_NUMBER() OVER(ORDER BY [FactoryNumber] ASC) AS Num FROM tb_AlarminstrumentInfo ";

                _SqlScript += $"    Where  [InstrumentId] > '0' ";

                //出厂编号
                if (logisticList.ContainsKey("FactoryNumber") && !string.IsNullOrEmpty(logisticList["FactoryNumber"]))
                {
                    _SqlScript += $"   And [FactoryNumber] like '%{logisticList["FactoryNumber"]}%'";
                }

                //生产厂家
                if (logisticList.ContainsKey("Manufacturer") && !string.IsNullOrEmpty(logisticList["Manufacturer"]))
                {
                    _SqlScript += $"   And [Manufacturer] like '%{logisticList["Manufacturer"]}%'";
                }

                //气体列表
                if (logisticList.ContainsKey("GasTypeList") && !string.IsNullOrEmpty(logisticList["GasTypeList"]))
                {
                    _SqlScript += $"   And [GasTypeList] like '%{logisticList["GasTypeList"]}%'";
                }

                //采样方式
                if (logisticList.ContainsKey("SampingMode") && !string.IsNullOrEmpty(logisticList["SampingMode"]))
                {
                    _SqlScript += $"   And [SampingMode] = '{logisticList["SampingMode"]}'";
                }

                //送检单位
                if (logisticList.ContainsKey("SubmitUnit") && !string.IsNullOrEmpty(logisticList["SubmitUnit"]))
                {
                    _SqlScript += $"   And [SubmitUnit] = '{logisticList["SubmitUnit"]}'";
                }

                //InstrumentId
                _SqlScript += $"    And [InstrumentId] In ( ";

                _SqlScript += $"    Select Distinct [InstrumentId] From  [tb_AlarminstrumentHisData]  Where [Id] > 0 ";

                if (logisticList.ContainsKey("BeginTime") && !string.IsNullOrEmpty(logisticList["BeginTime"]))
                {
                    _SqlScript += $"   And [CreateDate] >= '{logisticList["BeginTime"]}'";
                }

                if (logisticList.ContainsKey("FinishTime") && !string.IsNullOrEmpty(logisticList["FinishTime"]))
                {
                    _SqlScript += $"   And [CreateDate] <= '{logisticList["FinishTime"]}'";
                }

                if (logisticList.ContainsKey("UserId") && !string.IsNullOrEmpty(logisticList["UserId"]))
                {
                    _SqlScript += $"   And [UserId] = {logisticList["UserId"]}";
                }

                _SqlScript += $"    )  ";

                _SqlScript += $"    )AS TempTable";
                _SqlScript += $"    WHERE Num BETWEEN {startIndex} AND {startIndex + searchCount - 1}";



                _SqlDb = Static.Methods.OpenDataBase();
                _DataTable = _SqlDb.RunSqlScript(_SqlScript);

                _Result = DataTableToList(_DataTable);

                return _Result;
            }
            catch (Exception ex)
            {
                throw new Exception("获取报警器详细信息失败，错误信息：" + ex.Message);
            }
            finally
            {
                if (_SqlDb != null)
                    Static.Methods.CloseDataBase(_SqlDb);
            }
        }

        internal void UpdateAlarminstrumentInfo(Model.DB.tb_AlarminstrumentInfo alarminstrumentInfo)
        {
            Relations.DBUtility.SqlServer.SqlDb _SqlDb = null;
            string _SqlScript = string.Empty;
            try
            {
                _SqlScript += $"  Update tb_AlarminstrumentInfo Set InstrumentName = '{alarminstrumentInfo.InstrumentName}',FactoryNumber = '{alarminstrumentInfo.FactoryNumber}',Model = '{alarminstrumentInfo.Model}',ContractNo = '{alarminstrumentInfo.ContractNo}',SubmitUnit = '{alarminstrumentInfo.SubmitUnit}',Manufacturer = '{alarminstrumentInfo.Manufacturer}', GasTypeList = '{alarminstrumentInfo.GasTypeList}',SampingMode = {(int)alarminstrumentInfo.SampingMode}, Latitude = '{alarminstrumentInfo.Latitude}', Longitude = '{alarminstrumentInfo.Longitude}', GPSDesc = '{alarminstrumentInfo.GPSDesc}',ModifyDate = '{DateTime.Now.ToString(Static.ParaInfo.DateFormatLong)}'";
                _SqlScript += $"  Where [InstrumentId] = '{alarminstrumentInfo.InstrumentId}'";

                _SqlDb = Static.Methods.OpenDataBase();
                _SqlDb.UpdateSqlScript(_SqlScript);
            }
            catch (Exception ex)
            {
                throw new Exception("修改报警器失败，错误信息：" + ex.Message);
            }
            finally
            {
                if (_SqlDb != null)
                    Static.Methods.CloseDataBase(_SqlDb);
            }
        }

        string createInstrumentId()
        {
            return Guid.NewGuid().ToString();
        }

        internal Model.DB.tb_AlarminstrumentInfo GetInstrumentForFactoryNumberSingle(string FactoryNumber)
        {
            Relations.DBUtility.SqlServer.SqlDb sqlDb = null;
            try
            {
                DataTable _DataTable;
                string sqlScript;
                sqlScript = string.Format("Select * where FactoryNumber = '{0}';", FactoryNumber);
                sqlDb = Static.Methods.OpenDataBase();
                _DataTable = sqlDb.RunSqlScript(sqlScript);

                return DataTableToList(_DataTable)[0];
            }
            catch (Exception ex)
            {
                throw new Exception("获取被检仪器详细信息失败，错误信息：" + ex.Message);
            }
            finally
            {
                if (sqlDb != null)
                    Static.Methods.CloseDataBase(sqlDb);
            }
        }

        internal List<Model.DB.tb_AlarminstrumentInfo> GetInstrumentForFactoryNumberMore(List<string> listFactoryNumber)
        {

            Relations.DBUtility.SqlServer.SqlDb _SqlDb = null;
            List<Model.DB.tb_AlarminstrumentInfo> _Result;
            Model.DB.tb_AlarminstrumentInfo _tb_AlarminstrumentInfo = null;
            DataTable _DataTable;
            string _SqlScript;
            string _FactoryNumbers = string.Empty;

            if (listFactoryNumber.Count <= 0)
                return new List<Model.DB.tb_AlarminstrumentInfo>();

            try
            {
                _Result = new List<Model.DB.tb_AlarminstrumentInfo>();

                _FactoryNumbers = Static.Methods.CombiningStrings1<string>(listFactoryNumber);

                _SqlScript = string.Format("Select * From tb_AlarminstrumentInfo  ");
                _SqlScript += $"    where FactoryNumber in ({_FactoryNumbers}) ";
                _SqlScript += "    Order By FactoryNumber";
                _SqlDb = Static.Methods.OpenDataBase();
                _DataTable = _SqlDb.RunSqlScript(_SqlScript);

                _Result = DataTableToList(_DataTable);

                return _Result;
            }
            catch (Exception ex)
            {
                throw new Exception("获取被检仪器详细信息失败，错误信息：" + ex.Message);
            }
            finally
            {
                if (_SqlDb != null)
                    Static.Methods.CloseDataBase(_SqlDb);
            }
        }


        internal Model.DB.tb_AlarminstrumentInfo GetInstrumentForInstrumentIdSingle(string InstrumentId)
        {
            Relations.DBUtility.SqlServer.SqlDb sqlDb = null;
            try
            {
                DataTable _DataTable;
                string sqlScript;
                sqlScript = string.Format("Select * From tb_AlarminstrumentInfo where InstrumentId = '{0}';", InstrumentId);
                sqlDb = Static.Methods.OpenDataBase();
                _DataTable = sqlDb.RunSqlScript(sqlScript);

                return DataTableToList(_DataTable)[0];
            }
            catch (Exception ex)
            {
                throw new Exception("获取被检仪器详细信息失败，错误信息：" + ex.Message);
            }
            finally
            {
                if (sqlDb != null)
                    Static.Methods.CloseDataBase(sqlDb);
            }
        }


        internal string GetInstrumentIdForFactoryNumber(string factoryNumber)
        {
            Relations.DBUtility.SqlServer.SqlDb sqlDb = null;
            try
            {
                string sqlScript;
                string _Result = string.Empty;
                sqlScript = string.Format("Select InstrumentId From tb_AlarminstrumentInfo where FactoryNumber = '{0}';", factoryNumber);
                sqlDb = Static.Methods.OpenDataBase();


                _Result = sqlDb.RunSqlScriptReturnScalar(sqlScript);

                return _Result;
            }
            catch (Exception ex)
            {
                throw new Exception("获取被检仪器ID失败，错误信息：" + ex.Message);
            }
            finally
            {
                if (sqlDb != null)
                    Static.Methods.CloseDataBase(sqlDb);
            }
        }


        internal void updateGPS(string FactoryNumber, float latitude, float longitude, string desc, string uploadDate)
        {
            Relations.DBUtility.SqlServer.SqlDb sqlDb = null;
            try
            {
                string sqlScript = string.Format("update tb_AlarminstrumentInfo set GPSInfo = '{0}',uploadDate = '{2}' where FactoryNumber = '{1}';", latitude.ToString() + "|" + longitude.ToString() + "|" + desc, FactoryNumber, uploadDate);
                sqlDb = Static.Methods.OpenDataBase();
                sqlDb.UpdateSqlScript(sqlScript);
            }
            catch (Exception ex)
            {
                throw new Exception("更新仪器GPS信息失败，错误信息：" + ex.Message);
            }
            finally
            {
                if (sqlDb != null)
                    Static.Methods.CloseDataBase(sqlDb);
            }
        }

        internal void SaveCheckInstrument(Model.DB.tb_AlarminstrumentInfo alarminstrumentInfo)
        {
            Relations.DBUtility.SqlServer.SqlDb _SqlDb = null;
            string _SqlScript = string.Empty;
            try
            {
                //_SqlScript += "Insert Into tb_AlarminstrumentInfo";
                //_SqlScript += $"    Values ('{createInstrumentId()}','{alarminstrumentInfo.InstrumentName}','{alarminstrumentInfo.FactoryNumber}','{alarminstrumentInfo.Model}','{alarminstrumentInfo.ContractNo}','{alarminstrumentInfo.SubmitUnit}','{alarminstrumentInfo.Manufacturer}','{alarminstrumentInfo.GasTypeList}',{(int)alarminstrumentInfo.SampingMode},'{alarminstrumentInfo.ImageInfo}',{Static.ParaInfo.CurrentUser.userId},'{alarminstrumentInfo.GPSInfo}',Null,'{DateTime.Now.ToString(Static.ParaInfo.DateFormatLong)}',Null)";

                _SqlScript += "Insert Into tb_AlarminstrumentInfo (InstrumentName,FactoryNumber,Model,ContractNo,SubmitUnit,Manufacturer,GasTypeList,SampingMode,InputUserId,Latitude,Longitude,GPSDesc,ModifyDate,CreateDate,uploadDate)";
                _SqlScript += $"    Values ('{alarminstrumentInfo.InstrumentName}','{alarminstrumentInfo.FactoryNumber}','{alarminstrumentInfo.Model}','{alarminstrumentInfo.ContractNo}','{alarminstrumentInfo.SubmitUnit}','{alarminstrumentInfo.Manufacturer}','{alarminstrumentInfo.GasTypeList}',{(int)alarminstrumentInfo.SampingMode},{Static.ParaInfo.CurrentUser.userId},'{alarminstrumentInfo.Latitude}','{alarminstrumentInfo.Longitude}','{alarminstrumentInfo.GPSDesc}',Null,'{DateTime.Now.ToString(Static.ParaInfo.DateFormatLong)}',Null)";

                _SqlDb = Static.Methods.OpenDataBase();
                _SqlDb.UpdateSqlScript(_SqlScript);
            }
            catch (Exception ex)
            {
                throw new Exception("保存报警器失败，错误信息：" + ex.Message);
            }
            finally
            {
                if (_SqlDb != null)
                    Static.Methods.CloseDataBase(_SqlDb);
            }
        }

        internal void DeleteAlarminstrumentInfo(List<string> listInstrumentId)
        {
            Relations.DBUtility.SqlServer.SqlDb _SqlDb = null;
            string _InstrumentIds = string.Empty;
            string _SqlScript = string.Empty;
            try
            {
                _SqlDb = Static.Methods.OpenDataBase();
                _InstrumentIds = Static.Methods.CombiningStrings1<string>(listInstrumentId);

                _SqlScript = "Delete From tb_AlarminstrumentInfo";
                _SqlScript += $" Where InstrumentId in ( {_InstrumentIds} )";

                _SqlDb.UpdateSqlScript(_SqlScript);
            }
            catch (Exception ex)
            {
                throw new Exception("删除报警器信息失败，错误信息：" + ex.Message);
            }
            finally
            {
                if (_SqlDb != null)
                    Static.Methods.CloseDataBase(_SqlDb);
            }
        }

        internal List<GPSInfo> GetGPSInfo()
        {
            Relations.DBUtility.SqlServer.SqlDb _SqlDb = null;
            List<Model.DB.GPSInfo> _Result;
            Model.DB.GPSInfo _Temp = null;
            DataTable _DataTable;
            string _SqlScript;
            try
            {
                _Result = new List<GPSInfo>();

                _SqlScript = "Select InstrumentId,InstrumentName,Latitude,Longitude,GPSDesc,uploadDate From tb_AlarminstrumentInfo Order By InstrumentId";

                _SqlDb = Static.Methods.OpenDataBase();

                _DataTable = _SqlDb.RunSqlScript(_SqlScript);
                for (int i = 0; i < _DataTable.Rows.Count; i++)
                {
                    if (!Relations.Common.Methods.IsFloat(_DataTable.Rows[i]["Latitude"].ToString()) || !Relations.Common.Methods.IsFloat(_DataTable.Rows[i]["Longitude"].ToString())) continue;

                    _Temp = new Model.DB.GPSInfo();
                    _Temp.Id = _DataTable.Rows[i]["InstrumentId"].ToString();
                    _Temp.Name = _DataTable.Rows[i]["InstrumentName"].ToString();
                    _Temp.Latitude = float.Parse(_DataTable.Rows[i]["Latitude"].ToString());
                    _Temp.Longitude = float.Parse(_DataTable.Rows[i]["Longitude"].ToString());
                    _Temp.GpsDesc = _DataTable.Rows[i]["GPSDesc"].ToString();


                    if (!string.IsNullOrEmpty(_DataTable.Rows[i]["uploadDate"].ToString()))
                        _Temp.UploadDate = DateTime.Parse(_DataTable.Rows[i]["uploadDate"].ToString()).ToString(Static.ParaInfo.DateFormatLong);

                    _Result.Add(_Temp);
                }
                return _Result;
            }
            catch (Exception ex)
            {
                throw new Exception("获取报警器GPS信息失败，错误信息：" + ex.Message);
            }
            finally
            {
                if (_SqlDb != null)
                    Static.Methods.CloseDataBase(_SqlDb);
            }
        }

        private List<Model.DB.tb_AlarminstrumentInfo> DataTableToList(DataTable dataTable)
        {
            List<Model.DB.tb_AlarminstrumentInfo> _List;
            Model.DB.tb_AlarminstrumentInfo _AlarminstrumentInfo;
            Model.DB.tb_User _User;
            string _GasType;
            Model.DB.tb_ParaInfo _tb_ParaInfo;
            string[] _GasTypeList;

            try
            {
                _List = new List<Model.DB.tb_AlarminstrumentInfo>();

                for (int i = 0; i < dataTable.Rows.Count; i++)
                {
                    _AlarminstrumentInfo = null;
                    _AlarminstrumentInfo = new Model.DB.tb_AlarminstrumentInfo();
                    _AlarminstrumentInfo.InstrumentId = dataTable.Rows[i]["InstrumentId"].ToString();
                    _AlarminstrumentInfo.InstrumentName = dataTable.Rows[i]["InstrumentName"].ToString();
                    _AlarminstrumentInfo.FactoryNumber = dataTable.Rows[i]["FactoryNumber"].ToString();
                    _AlarminstrumentInfo.Model = dataTable.Rows[i]["Model"].ToString();
                    _AlarminstrumentInfo.ContractNo = dataTable.Rows[i]["ContractNo"].ToString();
                    _AlarminstrumentInfo.SubmitUnit = dataTable.Rows[i]["SubmitUnit"].ToString();
                    _AlarminstrumentInfo.Manufacturer = dataTable.Rows[i]["Manufacturer"].ToString();

                    _AlarminstrumentInfo.GasTypeList = dataTable.Rows[i]["GasTypeList"].ToString();
                    _GasType = dataTable.Rows[i]["GasTypeList"].ToString();
                    if (!string.IsNullOrWhiteSpace(_GasType))
                    {
                        _GasTypeList = _GasType.Split('|');
                        foreach (string _Gas in _GasTypeList)
                        {
                            _tb_ParaInfo = new DataAccess.tb_ParaInfo().GetParaInfo(_Gas);
                            _AlarminstrumentInfo.GasInfoList.Add(_tb_ParaInfo);
                            _AlarminstrumentInfo.sGasTypeList += _tb_ParaInfo.Desc + "|";
                        }
                        if (_AlarminstrumentInfo.sGasTypeList.EndsWith("|"))
                            _AlarminstrumentInfo.sGasTypeList = _AlarminstrumentInfo.sGasTypeList.Substring(0, _AlarminstrumentInfo.sGasTypeList.Length - 1);
                    }

                    _AlarminstrumentInfo.SampingMode = (RelationsLMP.Model.SystemEnum.AlertorSampingMode)int.Parse(dataTable.Rows[i]["SampingMode"].ToString());
                    _AlarminstrumentInfo.sSampingMode = Relations.Common.Methods.GetEnumDescription(_AlarminstrumentInfo.SampingMode);

                    _AlarminstrumentInfo.ImageInfo = dataTable.Rows[i]["ImageInfo"].ToString();

                    _AlarminstrumentInfo.InputUserId = int.Parse(dataTable.Rows[i]["InputUserId"].ToString());

                    _User = new tb_User().GetForUserId(_AlarminstrumentInfo.InputUserId);

                    if (_User != null && !string.IsNullOrEmpty(_User.userName))
                    {
                        _AlarminstrumentInfo.InputUserName = _User.userName;
                    }
                    else
                        _AlarminstrumentInfo.InputUserName = "未知";

                    _AlarminstrumentInfo.GPSDesc = dataTable.Rows[i]["GPSDesc"].ToString();
                    _AlarminstrumentInfo.Latitude = dataTable.Rows[i]["Latitude"].ToString();
                    _AlarminstrumentInfo.Longitude = dataTable.Rows[i]["Longitude"].ToString();

                    if (!string.IsNullOrEmpty(dataTable.Rows[i]["ModifyDate"].ToString()))
                        _AlarminstrumentInfo.ModifyDate = DateTime.Parse(dataTable.Rows[i]["ModifyDate"].ToString()).ToString(Static.ParaInfo.DateFormatLong);

                    if (!string.IsNullOrEmpty(dataTable.Rows[i]["CreateDate"].ToString()))
                        _AlarminstrumentInfo.CreateDate = DateTime.Parse(dataTable.Rows[i]["CreateDate"].ToString()).ToString(Static.ParaInfo.DateFormatLong); ;

                    _List.Add(_AlarminstrumentInfo);
                }

                return _List;
            }
            catch (Exception ex)
            {
                throw new Exception("将Datatable转换成List<Model.DB.tb_AlarminstrumentInfo>有异常：" + ex.ToString());
            }
        }
    }
}